//
//  DWJStatusToolbar.swift
//  weibo
//
//  Created by 代伟佳 on 16/4/21.
//  Copyright © 2016年 代伟佳. All rights reserved.
//

import UIKit

class DWJStatusToolbar: UIView {

    
    var status: DWJStatus? {
    
        didSet{
        

//            retweetButton.textLabel = setCountString(status?.reposts_count ?? 0, title: "转发")
//            commentButton.textLabel = setCountString(status?.comments_count ?? 0, title: "评论")
//            unlikeButton.textLabel = setCountString(status?.attitudes_count ?? 0, title: "赞")

        }
    }
    
    func setCountString(count: Int, title: String) -> String{
    
        if count <= 0 {
            
            return title
        } else if count > 0 && count < 10000 {
        
            return "\(count)"
        } else {
        
            let num = CGFloat(count / 1000) / 10
         
            // Int 转字符串
            var countString = "\(num)万"
            
            if countString.containsString(".0") {
            
                countString = countString.stringByReplacingOccurrencesOfString(".0万", withString: "万")
            }
            
            return countString
        }
        
    }
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    // MARK: - 设置视图
    private func setUpUI() {
    
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        let retweetButton = addChildButton("timeline_icon_retweet", title: "转发")
        let commentButton = addChildButton("timeline_icon_comment", title: "评论")
        let unlikeButton = addChildButton("timeline_icon_unlike", title: "赞")
        
        // 两根竖线
        let line1 = addChildLineImageView()
        let line2 = addChildLineImageView()
        
        // 02-添加约束
        
        // 01
        retweetButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.leading.equalTo(self)
            make.width.equalTo(commentButton)
        }
        
        // 02
        commentButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retweetButton.snp_trailing)
            make.width.equalTo(unlikeButton)
        }
        
        // 03
        unlikeButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(commentButton.snp_trailing)
        }
        
        // 04
        line1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton.snp_trailing)
            make.centerY.equalTo(retweetButton)
        }
        
        // 05
        line2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton.snp_trailing)
            make.centerY.equalTo(commentButton)
        }
        
        
    }
    
    
    func addChildButton(imageName: String, title: String) -> UIButton{
    
        let button = UIButton()
        
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        addSubview(button)

        return button
    }
    
    // 创建竖线
    func addChildLineImageView() -> UIImageView{
        let img = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(img)
        return img
    }

}
